package cn.liwq.demo.service.excel.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lweiqiang
 * @date 2022/12/3
 */
@Slf4j
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {

    private static final String DATE_MARK = "Date";

    private Integer realGoodsIndexBegin;
    private Map<Integer, String> indexRealGoodsMap = MapUtils.newHashMap();
    private Map<String, TreeMap<Date, BigDecimal>> realGoodsMap;

    private Integer paperGoodsIndexBegin;
    private Map<Integer, String> indexPaperGoodsMap = MapUtils.newHashMap();
    private Map<String, TreeMap<Date, BigDecimal>> paperGoodsMap;

    @Override

    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        data.forEach((idx, name) -> {
            if (name != null && name.equals(DATE_MARK)) {
                if (realGoodsIndexBegin == null) {
                    realGoodsIndexBegin = idx;
                } else {
                    paperGoodsIndexBegin = idx;
                }
            }
        });
        if (data.containsKey(0) && data.get(0).equals(DATE_MARK)) {
            indexRealGoodsMap = data.entrySet().stream()
                    .filter(e -> e.getValue() != null && !e.getValue().equals(DATE_MARK))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
            log.info("解析到产品名称：{}", JSON.toJSONString(indexRealGoodsMap));
            realGoodsMap = MapUtils.newHashMapWithExpectedSize(indexRealGoodsMap.size());
            indexRealGoodsMap.values().forEach(realGoods -> realGoodsMap.put(realGoods, new TreeMap<>()));
        }
        if (!data.isEmpty()) {

        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成！");
    }
}
